#include "vtor_i2c.h"

// 加入需要依赖的头文件
#include "main.h"

// 加入需要依赖的变量

#define	SI2C2_SCL_Port		GPIOB
#define SI2C2_SCL_Pin		GPIO_PIN_10
//#define SI2C2_SCL_Pin		GPIO_PIN_6
#define	SI2C2_SDA_Port		GPIOB
#define SI2C2_SDA_Pin		GPIO_PIN_11
//#define SI2C2_SDA_Pin		GPIO_PIN_7


struct VtorI2c si2c2;


void si2c2_setSCL(char level)
{
	HAL_GPIO_WritePin(SI2C2_SCL_Port, SI2C2_SCL_Pin, level);
}
char si2c2_readSCL()
{
	return HAL_GPIO_ReadPin(SI2C2_SCL_Port, SI2C2_SCL_Pin);;
}

void si2c2_setSDA(char level)
{
	HAL_GPIO_WritePin(SI2C2_SDA_Port, SI2C2_SDA_Pin, level);
}

char si2c2_readSDA()
{
	return HAL_GPIO_ReadPin(SI2C2_SDA_Port, SI2C2_SDA_Pin);;
}

void si2c2_setSDADir(char dir)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin = SI2C2_SDA_Pin;
	if(dir == GPIO_DIR_OUTPUT)
	{
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	}
	else
	{
		GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	}
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(SI2C2_SDA_Port, &GPIO_InitStruct);
}









// 初始化IIC的IO口 给函数指针赋值
void VtorI2c_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	/*Configure GPIO pins : PBPin PBPin PBPin */
	GPIO_InitStruct.Pin = SI2C2_SCL_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	HAL_GPIO_Init(SI2C2_SCL_Port, &GPIO_InitStruct);

	/*Configure GPIO pins : PBPin PBPin PBPin */
	GPIO_InitStruct.Pin = SI2C2_SDA_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	HAL_GPIO_Init(SI2C2_SDA_Port, &GPIO_InitStruct);


	si2c2.setSCL = si2c2_setSCL;
	si2c2.readSCL = si2c2_readSCL;
	si2c2.setSDA = si2c2_setSDA;
	si2c2.readSDA = si2c2_readSDA;
	si2c2.setSDA_Dir = si2c2_setSDADir;
    VtorI2c_Stop(&si2c2);
}


inline void VtorI2c_Delay(uint32_t tick)
{
	//__NOP();
	//HAL_Delay(tick/4);
}




